#include<iostream>
#include<func.h>
#include<string.h>
using std::cout;
using std::endl;
//Node 节点
class Node{
public:
    Node(int num)
    :_num(num)
    {
        _pNext->_num = num;
        _pNext->_pNext = nullptr;
    }
    ~Node(){
        if(_pNext){
            delete _pNext;
            _pNext = nullptr;
            cout<<"this is ~Node"<<endl;
        }
    }
    Node *_pNext = (Node*)malloc(sizeof(Node));
    int _num;
    //Node *pNext = new Node();  错误原因 new 会调用类的构造
};

class Stack {
public:
    //栈的构造函数
    Stack();
    //栈的析构函数
    ~Stack();
    void print();
	bool empty();	//判断栈是否为空
	bool full();	//判断栈是否已满
	void push(int num); //元素入栈
	void pop();     //元素出栈
	int  top();		//获取栈顶元素
    void visit();
    void clear();
//...
private:
    int _length; //stack 栈的长度
    Node* pHead; //头节点
};
//构造函数初始化
Stack::Stack()
{
    _length = 0;
    pHead =nullptr;
    cout<<"stack is ready"<<endl;
}
//empty
bool Stack::empty(){
    if(_length == 0)
    {
        return true;
    }
    return false;
}
//PUSH
void Stack::push(int num)
{
    Node *pNnew = new Node(num);
    pNnew->_pNext = pHead;
    pHead = pNnew;
    _length++;
}
//pop
void Stack::pop(){
    if(_length!=0)
    {
        Node*tmp = pHead;
        pHead = tmp->_pNext;
        delete tmp;
        tmp = nullptr;
        _length--;
    }
}
//visit
void Stack::visit(){
    Node *pTemp = pHead;
    while(pTemp){
        cout<< pTemp->_num <<endl;
        pTemp = pTemp->_pNext;
    }
    delete pTemp;
    pTemp =nullptr;
}
//print
void Stack:: print(){
    cout<<"stack length : "<<_length<<endl;
    if(pHead != nullptr)
    {
        cout<<"stack pHead :"<<pHead->_num<<endl;
    }
    else{
        cout<<"print is null"<<endl;
    }
}
//top
int Stack::top(){
    int num = pHead->_num;
    return num;
}

//清空栈
void Stack::clear(){
    while(pHead->_pNext!=nullptr)
    {
        if(pHead!=nullptr){
            Node *node = pHead->_pNext;
            pHead->_pNext = node->_pNext;
            delete(node);
        }
    }
}
//~stack
Stack::~Stack(){
    clear();
    delete pHead;
    pHead = nullptr;
    cout<<"~Stack"<<endl;
}

int main(void)
{
    Stack stack;
    stack.push(3);
    stack.push(4);
    return 0;
}